package com.bz.sunmipay.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.baizhi.baseapp.activity.BaseActivity;
import com.bz.sunmipay.R;
import com.bz.sunmipay.utils.AppUtils;
import com.bz.sunmipay.utils.ArithmeticUtils;
import com.bz.sunmipay.utils.IntentUtils;

/**
 * 支付
 * <p>
 * Created by liuyakui on 2019-09-07.
 */
public class PaymentActivity extends BaseActivity implements View.OnClickListener {

    TextView payment_tv_vat;
    EditText payment_et_vat;
    Button payment_btn_cash;
    Button payment_btn_card;
    EditText payment_et_amount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_payment);
        setLeftContentIcon(R.mipmap.back);
        setRightContentIcon(R.mipmap.home);
        setCenterText("PAYMENT");

        initContentView();
    }

    @Override
    protected void handleClickEvent(int event) {
        super.handleClickEvent(event);
        if (event == RIGHT_BUTTON) {
            IntentUtils.goToHome(this);
        }
    }

    private void initContentView() {
        payment_tv_vat = findViewById(R.id.payment_tv_vat);
        payment_et_vat = findViewById(R.id.payment_et_vat);
        payment_et_amount = findViewById(R.id.payment_et_amount);
        payment_btn_cash = findViewById(R.id.payment_btn_cash);
        payment_btn_card = findViewById(R.id.payment_btn_card);
        payment_btn_cash.setOnClickListener(this);
        payment_btn_card.setOnClickListener(this);
        payment_et_amount.requestFocus();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                showSoftKeyboard(payment_et_amount, PaymentActivity.this);
            }
        }, 100);
    }

    private void showSoftKeyboard(View view, Context mContext) {
        if (view.requestFocus()) {
            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.payment_btn_cash:
                jumpChargeActivity(IntentUtils.PAGE_FROM_CASH);
                break;
            case R.id.payment_btn_card:
                String amount = payment_et_amount.getText().toString().trim();
                if (verifyInputAmount(amount)) {
                    showMessage(getResources().getString(R.string.payment_amount_error));
                    return;
                }
                amount = ArithmeticUtils.convertBigDecimal(amount);

                final String vat = payment_et_vat.getText().toString().trim();
                if (verifyInputAmount(vat)) {
                    showMessage(getResources().getString(R.string.payment_vat_error));
                    return;
                }
                float parseLong = AppUtils.convertToFloat(amount, 0);
                if (parseLong <= 0) {
                    Toast.makeText(this, "Please enter the correct consumption amount", Toast.LENGTH_SHORT).show();
                    return;
                }
                IntentUtils.goToPaymentPatternPage(this, amount, vat);
                break;
        }
    }

    private boolean verifyInputAmount(String val){
        if (TextUtils.isEmpty(val) || ArithmeticUtils.convertToInt(val) == 0){
            return true;
        }
        return false;
    }

    private void jumpChargeActivity(int type){
        String amountCard = payment_et_amount.getText().toString().trim();
        if (verifyInputAmount(amountCard)) {
            showMessage(getResources().getString(R.string.payment_amount_error));
            return;
        }
        amountCard = ArithmeticUtils.convertBigDecimal(amountCard);
        final String vatCard = payment_et_vat.getText().toString().trim();
        if (verifyInputAmount(vatCard)) {
            showMessage(getResources().getString(R.string.payment_vat_error));
            return;
        }
        IntentUtils.goToPaymentCharge(PaymentActivity.this, amountCard, vatCard, type);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1001 && resultCode == 1) {
            finish();
        }
    }
}
